package com.example.Controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @RequestMapping("/login")
    public String login(String username, String password, Model model){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();

        //封装用户登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);

        try{
            //执行登录方法,如果没有异常就登录
            subject.login(token);
            return "index";
        }catch (UnknownAccountException e){//登录异常，用户名错误
            model.addAttribute("msg"," 用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){//登录异常，密码错误
            model.addAttribute("msg"," 密码错误");
            return "login";
        }

    }

}
